package org.mygui.mongo;


import java.util.*;

import org.bson.Document;
import org.mygui.bean.DBInfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

public class MyMongoDBClient {
	
	private static DBInfo dbInfo;
	public static void setDBInfo(DBInfo dbInfo1){
		dbInfo = dbInfo1;
	}
	public static MongoDatabase getDataBaseByDB(){
		MongoClient mongoClient = null;
		MongoDatabase mongoDatabase = null;
		try{   
			// 连接到 mongodb 服务
	        mongoClient = new MongoClient(dbInfo.getIp(), dbInfo.getPort());
	        // 连接到数据库
	        mongoDatabase = mongoClient.getDatabase(dbInfo.getDbName());
		}catch(Exception e){
		    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
		}
		return mongoDatabase;
	}
	/**
	 * 获取数据库
	 * @param url
	 * @param port
	 * @param dbName
	 * @return
	 */
	public static MongoDatabase getDataBase(String url,int port,String dbName){
		MongoClient mongoClient = null;
		MongoDatabase mongoDatabase = null;
		try{   
			// 连接到 mongodb 服务
	        mongoClient = new MongoClient(url, port);
	        // 连接到数据库
	        mongoDatabase = mongoClient.getDatabase(dbName);
		}catch(Exception e){
		    System.err.println( e.getClass().getName() + ": " + e.getMessage() );
		}
		return mongoDatabase;
	}
	/**
	 * 获取所有连接  "suggestion"
	 * @param mongoDatabase
	 */
	public static List<String> getList(MongoDatabase mongoDatabase,String collectionName){
		List<String> list = new ArrayList<String>();
		MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
		FindIterable<Document> findIterable = collection.find();  
        MongoCursor<Document> mongoCursor = findIterable.iterator();  
        while(mongoCursor.hasNext()){  
        	list.add(mongoCursor.next().toJson());  
        } 
        return list;
	}
	/**
	 * 条件查询
	 * @param database
	 */
	public static List<String> find(MongoDatabase database,String colName,List<QueryParam> paramList){
		List<String> list = new ArrayList<String>();
		MongoCollection<Document> collection = database.getCollection(colName);
	    BasicDBObject query = new BasicDBObject();
	    for(QueryParam params:paramList){
	    	if(0==params.type){
	    		query.put(params.param, Integer.parseInt(params.val));
	    	}else if(1==params.type){
	    		query.put(params.param, params.val);
	    	}
	    }
	    FindIterable<Document> findIterable = collection.find(query);
	    MongoCursor<Document> mongoCursor = findIterable.iterator();  
	    while(mongoCursor.hasNext()){
	    	list.add(mongoCursor.next().toJson());
	    }
	    return list;
	}
	public static List<QueryParam> getQueryParams(String param){
		System.out.println("param="+param);
		List<QueryParam> list = new ArrayList<QueryParam>();
		if(null!=param&&!"".equals(param.trim())){
			String[] params = param.split(",");
			for(String s:params){
				list.add(getQueryParam(s));
			}
		}
		return list;
	}
	public static QueryParam getQueryParam(String param){
		System.out.println("turn map="+param);
		if(null!=param&&!"".equals(param.trim())){
			QueryParam q = new QueryParam();
			if(param.indexOf("\"")>-1){
				q.type = 1;
			}else{
				q.type = 0;
			}
			Map<String, Object> map = JSON.parseObject(
					param,new TypeReference<Map<String, Object>>(){});
			Set<String> set = map.keySet();
			for(String s:set){
				q.param = s;
				q.val = map.get(s).toString();
			}
			
			return q;
		}
		return null;
	}
	static class QueryParam{
		Integer type;
		String param;
		String val;
	}
	public static void main(String[] args) {
//		find(getDataBase("192.168.102.65",47017,"dxh"),"suggestion",getQueryParam(""));
//		getQueryParam("");
	}
}
